<?php
namespace Mob\Model;

use Think\Model;

/**群组模型，通过该模型可以对群组进行操作
 * DiscussGroup DataModel
 */
require_once ('./Application/Mob/Common/function.php');

class DiscussGroupModel extends Model
{
     /**
     * 创建群组
     * @param varchar $businessId  业务流水号
     * @param int $Uid 用户ID
     * @param int $is_Sponsor 是否为群组创建者1=是,0=否
     * @param char $longitude longitude
     * @param char $latitude latitude
     * @param char $step 当前环节
     * @param bigint $groupId 组ID， 如果存在组ID，代表是加入现有的组，如果没有的话，则为新创建一个群组
     * @return  groupId  返回群组ID
     * @author
     */
  public function createGroup($businessId,$Uid,$is_Sponsor,$longitude,$latitude,$step,$groupId=null)
  {
    $groupId=empty($groupId)?getSerialno(20):$groupId;
    $data['groupId']=$groupId;
    $data['businessId']=$businessId;
    $data['groupType']=1;
    $data['Uid']=$Uid;
    $data['addTime']=time();
    $data['is_Sponsor']=$is_Sponsor;
    $data['longitude']=$longitude;
    $data['latitude']=$latitude;
    $data['step']=$step;
    $data['status']=1;   //群组状态1=在用,2=退出
    $data['lastOperTime']=time(); //更新最后操作时间为当前时间
    if($this->add($data))
    {
    return $data['groupId'];
    }

  }
     /**
     * 退出群组
     * @param bigint $groupId 组ID
     * @param int $Uid 用户ID
     * @return true=退出成功,false=退出失败
     * @author
     */
  public function exitGroup($groupId,$Uid)
  {
  $where = array('groupId'=>$groupId,'Uid'=>$Uid);
  $data= array('exitTime'=>time(),'status'=>2);
  return $this->where($where)->save($data);
  }
     /**
     * 我的群组
     * @param int $Uid 用户ID
     * @return array  群组信息
     * @author
     */
  public function get_MyGroups($uid)
  {
   $where = array('Uid'=>$uid,'status'=>1);
   return $this->where($where)->field('groupId,businessId,groupType,Uid,addTime,is_Sponsor,longitude,latitude,step')->order('lastOperTime desc')->select();

  }
     /**
     * 获取群组内人员
     * @param int $Uid 用户ID
     * @return array  群组信息
     * @author
     */
  public function get_GroupPeopleInfo($groupId)
  {
   $where = array('groupId'=>$groupId,'status'=>1);
   $groupPeople = $this->where($where)->field('groupId,businessId,groupType,Uid,addTime,is_Sponsor,longitude,latitude,step')->order('lastOperTime ')->select();

   foreach($groupPeople as &$v)
   {
   $v['user'] = query_user(array('nickname', 'avatar64', 'uid'), $v['Uid']);

   }
   unset($v);
   return $groupPeople;

  }
     /**
     * 更新群组操作时间
     * @param bigint $groupId 组ID
     * @param int $Uid 用户ID
     * @return  true=更新成功,false=更新失败
     * @author
     */
  public function updateOperateTime($groupId,$uid)
  {
  $where = array('groupId'=>$groupId,'Uid'=>$uid,'status'=>1);
  return $this->where($where)->save(array('lastOperTime'=>time()));

  }

     /**
     * 根据业务ID和环节、地理位置，获取附近同一招聘环节的群组ID
     * @param varchar $businessId 业务流水号
     * @param int $step 当前环节
     * @param char $longitude longitude
     * @param char $latitude latitude
     * @param int $distance 距离，默认为60米
     * @return  groupId 群组ID，如果附近有群组则返回群组ID，没有则返回为空
     * @author
     */
  public function is_NearbyGroup($businessId,$step,$longitude,$latitude,$distance=60)
  {
    $group = new \ Think \ Model();
   return  $group->query("select * from (select businessId, step, status,groupId,groupType,Uid,addTime,longitude,latitude,round(6378.138*2*asin(sqrt(pow(sin( (".$latitude."*pi()/180-latitude*pi()/180)/2),2)+cos(".$latitude."*pi()/180)*cos(latitude*pi()/180)* pow(sin( (".$longitude."*pi()/180-longitude*pi()/180)/2),2)))*1000) as distance " .
   		"from ocenter_discuss_group g where g.businessId = $businessId and step = $step and status =1 and is_Sponsor =1)  as discuss_group where distance <= $distance  order by distance limit 1")[0]['groupId'];

  }
     /**
     * 添加到群组,先判断附近是否有相关群组，如果有则直接加入到群组，没有则重新创建一个
     * @param varchar $businessId 业务流水号
     * @param int $Uid 用户ID
     * @param char $longitude longitude
     * @param char $latitude latitude
     * @param int $step 当前环节
     * @return  groupId 成功则返回群组ID
     * @author
     */
  public function addGroup($businessId,$Uid,$longitude,$latitude,$step)
  {
  $nearbyGroup = $this->is_NearbyGroup($businessId,$step,$longitude,$latitude);  //取附近地点是否有该招聘环节的群组

  if($nearbyGroup) //判断是否能返回附件的活动群组
  {
  $is_Sponsor=0;
  $groupId = $nearbyGroup;
  }else
  {
   $is_Sponsor=1;
  $groupId = null;
  }
  return $this->createGroup($businessId,$Uid,$is_Sponsor,$longitude,$latitude,$step,$groupId);  //执创创建方法

  }
     /**
     * 判断是否已经加入到指定群组内
     * @param bigint $groupId 业务流水号
     * @param int $Uid 用户ID
     * @return  array 如果存在则返回群组信息，否则为空
     * @author
     */
  public function is_AddGroup($groupId,$uid)
  {
  return $this->where(array('groupId'=>$groupId,'Uid'=>$uid,'status'=>1))->field('groupId,businessId,groupType,Uid,addTime,is_Sponsor,longitude,latitude,step')->find();

  }
     /**
     * 获取附近的群组
     * @param char $longitude longitude
     * @param char $latitude latitude
     * @param char $groupType 群组类型
     * @param char $distance 距离
     * @return  array 如果存在则返回群组信息，否则为空
     * @author
     */
  public function get_nearbyGroup($longitude,$latitude,$groupType=1,$distance=60)
  {
  	$group = new \ Think \ Model();
   return  $group->query("select * from (select groupType, status,is_Sponsor,businessId, step,groupId,Uid,addTime,longitude,latitude,round(6378.138*2*asin(sqrt(pow(sin( (".$latitude."*pi()/180-latitude*pi()/180)/2),2)+cos(".$latitude."*pi()/180)*cos(latitude*pi()/180)* pow(sin( (".$longitude."*pi()/180-longitude*pi()/180)/2),2)))*1000) as distance " .
   		"from ocenter_discuss_group g where g.groupType = $groupType and status =1 and is_Sponsor =1)  as discuss_group where distance <= $distance  order by distance ");


  }








}